<!DOCTYPE html>
<!-- https://blog.csdn.net/a872822645/article/details/85005144 -->
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>Excel批量处理-xlsx文件导入窗口</title>
    <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.js"></script>
    <script src="https://cdn.bootcss.com/xlsx/0.11.5/xlsx.core.min.js"></script>
    
</head>

<body>
    <input type="file" id="excel-file">
    <script>
        //给input标签绑定change事件，一上传选中的.xls文件就会触发该函数
        $('#excel-file').change(function (e) {
            var files = e.target.files;
            var fileReader = new FileReader();
            fileReader.onload = function (ev) {
                try {
                    var data = ev.target.result
                    var workbook = XLSX.read(data, {
                        type: 'binary'
                    }) // 以二进制流方式读取得到整份excel表格对象
                    var persons = []; // 存储获取到的数据
                } catch (e) {
                    //console.log('文件类型不正确');
                    return;
                }
                // 表格的表格范围，可用于判断表头是否数量是否正确
                var fromTo = '';
                // 遍历每张表读取
                for (var sheet in workbook.Sheets) {
                    if (workbook.Sheets.hasOwnProperty(sheet)) {
                        fromTo = workbook.Sheets[sheet]['!ref'];
                        var obj = XLSX.utils.sheet_to_json(workbook.Sheets[sheet])
                        persons = persons.concat(obj);
                        // break; // 如果只取第一张表，就取消注释这行
                    }
                }
                //在控制台打印出来表格中的数据
                //console.log("persons is ", persons);
                //console.log("==================== ");



                var mapList = []
                for (var index in persons) {
                    var chineseProvinceName =
                        mapList.push({
                            "chineseProvinceName": persons[index].chineseProvinceName,
                            "countNumber": persons[index].countNumber,
                            "englishProvinceName": persons[index].englishProvinceName
                        })
                }
                //console.log("mapList is ", mapList)
                sessionStorage.setItem("mapList", JSON.stringify(mapList));
                // */
                //跳转页面
                location.href = "map.html";
            };
            // 以二进制方式打开文件
            fileReader.readAsBinaryString(files[0]);
        });
    </script>
</body>

</html>